package com.kk.demo.java8.stream.base01;

import java.util.Arrays;
import java.util.List;

/**
 * 开始使用stream并带有reduce,并能进行并行计算
 * @author liujun
 * @date 2016年8月28日
 * @verion 0.0.1
 */
public class StreamUseReduceParalle {

    public StreamUseReduceParalle() {
    }

    public static void main(String[] args) {
        StreamUseReduceParalle stream = new StreamUseReduceParalle();

        stream.getStream();

    }

    /**
     * 得到Stream对象
     * @return
     */
    public void getStream() {
        // 声明一个集合
        List<Parson> arrayInt = Arrays.asList(new Parson("张三", 100), new Parson("李四", 200), new Parson("王五", 300));

        // 进行并行计算
        int value = arrayInt.stream().parallel().reduce(0, (sum1, p2) -> {
            System.out.println("当前线程:" + Thread.currentThread().getId() + ",p1:" + sum1 + ",p2:" + p2);
            return sum1 + p2.getValue();
        }, (sum1, sum2) -> {
            System.out.println("当前线程:" + Thread.currentThread().getId() + ",sum1:" + sum1 + ",sum2:" + sum2);
            return sum1 + sum2;
        });
        System.out.println("并行计算的结果:" + value);

        // 进行并行计算
        int value2 = arrayInt.stream().reduce(0, (sum1, p2) -> {
            System.out.println("当前线程:" + Thread.currentThread().getId() + ",p1:" + sum1 + ",p2:" + p2);
            return sum1 + p2.getValue();
        }, (sum1, sum2) -> {
            System.out.println("当前线程:" + Thread.currentThread().getId() + ",sum1:" + sum1 + ",sum2:" + sum2);
            return sum1 + sum2;
        });
        System.out.println("未并行计算的结果:" + value2);

    }

}
